package vno.ky.mapMaker
{
	import fl.controls.Button;
	
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.FullScreenEvent;
	import flash.events.MouseEvent;
	public class Buttons extends Sprite
	{
		public var coordinate:Array=new Array();
		public var output:Array;
		public var buttonWall:Button=new Button();
		public var buttonRoad:Button=new Button();
		public var buttonFinRoa:Button=new Button();
		public var buttonFinWal:Button=new Button();
		public var buttonout:Button=new Button();
		private var startX:int;
		private var startY:int;
		private var endX:int;
		private var endY:int;
		private var result:Array=new Array();
		private var resultCount:int =0;
		public function Buttons()
		{}
		public function startMaker():void
		{
			reset();
			ButWal();
			ButRoa();
			ButFinRoa();
			ButFinWal();
			Output();
		}
		public function reset():void
		{
			for(var lin:int=0;lin<=stage.stageWidth/Grip.pixel;lin++)
			{
				coordinate[lin]=new Array();
				for(var col:int=0;col<=stage.stageHeight/Grip.pixel;col++)
				{
					coordinate[lin][col]=100;						//100为空属性
				}
			}
		}
		public function ButWal():void							//设定墙按钮
		{
			buttonWall.move(10*Grip.pixel,0);
			buttonWall.label="set wall";
			buttonWall.width=15*Grip.pixel;
			buttonWall.height=10*Grip.pixel;
			addChild(buttonWall);
			buttonWall.addEventListener(MouseEvent.CLICK,buttonWallClick);
		}
		private function buttonWallClick(evt:MouseEvent):void			//点击墙按钮
		{
			this.stage.addEventListener(MouseEvent.MOUSE_DOWN,downHandler);
			this.stage.addEventListener(MouseEvent.MOUSE_UP,upHandler)
			function downHandler(evt:MouseEvent):void					//按下墙按钮后，获取起点终点坐标
			{
				if(evt.stageY>10*Grip.pixel)
				{
					startX=evt.stageX/Grip.pixel;
					startY=evt.stageY/Grip.pixel;
				}
			}
			function upHandler(evt:MouseEvent):void						//终点获取后，设置路点属性，画出相应图形
			{
				var temp1:int,temp2:int;
				if(evt.stageY>10*Grip.pixel)
				{
					endX=evt.stageX/Grip.pixel;
					endY=evt.stageY/Grip.pixel;
					temp1=Math.min(startX,endX);						//设定起始点为矩形左上和右下顶点
					temp2=Math.min(startY,endY);
					endX=Math.max(startX,endX);
					endY=Math.max(startY,endY);
					startX=temp1;
					startY=temp2;
					for(var lin:int=startX;lin<=endX;lin++)
					{
						for(var col:int=startY;col<=endY;col++)
						{
							coordinate[lin][col]=1;
							var rect:Shape=new Shape();
							rect.graphics.beginFill(0x00ffFF);
							rect.graphics.drawRect(lin*Grip.pixel,col*Grip.pixel,Grip.pixel,Grip.pixel);
							rect.graphics.endFill();
							addChild(rect);
						}
					}
					result[resultCount]=new int();			//五个一组输出结果（墙），前四个分别表示矩形左上和右下点坐标，第五个置0起分隔作用
					result[resultCount++]=startX;
					result[resultCount]=new int();
					result[resultCount++]=startY;
					result[resultCount]=new int();
					result[resultCount++]=endX;
					result[resultCount]=new int();
					result[resultCount++]=endY;
					result[resultCount]=new int();
					result[resultCount++]=0;
				}
			}
		}
		public function ButRoa():void							//设定路按钮
		{
			buttonRoad.move(30*Grip.pixel,0);
			buttonRoad.label="set road";
			buttonRoad.width=15*Grip.pixel;
			buttonRoad.height=10*Grip.pixel;
			addChild(buttonRoad);
			buttonRoad.addEventListener(MouseEvent.CLICK,buttonRoadClick);
		}
		private function buttonRoadClick(evt:MouseEvent):void			//点击路按钮
		{
			this.stage.addEventListener(MouseEvent.MOUSE_DOWN,downHandler);
			this.stage.addEventListener(MouseEvent.MOUSE_UP,upHandler)
			function downHandler(evt:MouseEvent):void
			{
				if(evt.stageY>10*Grip.pixel)
				{
					startX=evt.stageX/Grip.pixel;
					startY=evt.stageY/Grip.pixel;
				}
			}
			function upHandler(evt:MouseEvent):void
			{
				var temp1:int,temp2:int;
				if(evt.stageY>10*Grip.pixel)
				{
					endX=evt.stageX/Grip.pixel;
					endY=evt.stageY/Grip.pixel;
					temp1=Math.min(startX,endX);
					temp2=Math.min(startY,endY);
					endX=Math.max(startX,endX);
					endY=Math.max(startY,endY);
					startX=temp1;
					startY=temp2;
					for(var lin:int=startX;lin<=endX;lin++)
					{
						for(var col:int=startY;col<=endY;col++)
						{
							coordinate[lin][col]=0;
							var rect:Shape=new Shape();
							rect.graphics.beginFill(0xffcc00);
							rect.graphics.drawRect(lin*Grip.pixel,col*Grip.pixel,Grip.pixel,Grip.pixel);
							rect.graphics.endFill();
							addChild(rect);
						}
					}
				}
			}
		}
		public function ButFinRoa():void							//设定finishroad按钮
		{
			buttonFinRoa.move(50*Grip.pixel,0);
			buttonFinRoa.label="finish road";
			buttonFinRoa.width=20*Grip.pixel;
			buttonFinRoa.height=10*Grip.pixel;
			addChild(buttonFinRoa);
			buttonFinRoa.addEventListener(MouseEvent.CLICK,buttonFinRoaClick);
			output=coordinate.slice();								//输出数组为当前数组的复制
		}
		private function buttonFinRoaClick(evt:MouseEvent):void		//监听finishroad
		{
			for(var lin:int=0;lin<=stage.stageWidth/Grip.pixel;lin++)
			{
				for(var col:int=11;col<=stage.stageHeight/Grip.pixel;col++)
				{
					if(coordinate[lin][col]!=1&&coordinate[lin][col]!=0)
					{
						coordinate[lin][col]=0;
						var rect:Shape=new Shape();
						rect.graphics.beginFill(0xffcc00);
						rect.graphics.drawRect(lin*Grip.pixel,col*Grip.pixel,Grip.pixel,Grip.pixel);
						rect.graphics.endFill();
						addChild(rect);
					}
				}
			}
		}
		public function ButFinWal():void							//设定finishwall按钮
		{
			buttonFinWal.move(75*Grip.pixel,0);
			buttonFinWal.label="finish wall";
			buttonFinWal.width=20*Grip.pixel;
			buttonFinWal.height=10*Grip.pixel;
			addChild(buttonFinWal);
			buttonFinWal.addEventListener(MouseEvent.CLICK,buttonFinWalClick);
			output=coordinate.slice();
		}
		private function buttonFinWalClick(evt:MouseEvent):void
		{
			for(var lin:int=0;lin<=stage.stageWidth/Grip.pixel;lin++)
			{
				for(var col:int=11;col<=stage.stageHeight/Grip.pixel;col++)
				{
					if(coordinate[lin][col]!=1&&coordinate[lin][col]!=0)
					{
						coordinate[lin][col]=1;
						var rect:Shape=new Shape();
						rect.graphics.beginFill(0x00ffFF);
						rect.graphics.drawRect(lin*Grip.pixel,col*Grip.pixel,Grip.pixel,Grip.pixel);
						rect.graphics.endFill();
						addChild(rect);
					}
				}
			}
		}
		public function Output():void							//设定output按钮
		{
			buttonout.move(100*Grip.pixel,0);
			buttonout.label="output";
			buttonout.width=10*Grip.pixel;
			buttonout.height=10*Grip.pixel;
			addChild(buttonout);
			buttonout.addEventListener(MouseEvent.CLICK,buttonOutClick);
		}
		private function buttonOutClick(evt:MouseEvent):void
		{
			/*	for(var col:int=11;col<=stage.stageHeight/Grip.pixel;col++)
			{
			var str:Array=new Array();
			for(var lin:int=0;lin<=stage.stageWidth/Grip.pixel;lin++)
			{
			str[lin]=output[lin][col];
			}
			trace(str);
			}*/										//原output输出结果，输出所有点状态
			trace(result);
		}
	}
}